#!/bin/bash
#Descript: elasticsearch management script.
#Date: 2019-10-31

dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd ${dir}
config_file=${dir}/config/elasticsearch.yml
app_port=$(egrep '^http.port:' ${config_file} |awk '{print $2}')
app_data_dir=$(egrep '^path.data:' ${config_file} |awk '{print $2}')
app_log_dir=$(egrep '^path.logs:' ${config_file} |awk '{print $2}')
app_name='elasticsearch'
run_user='elastic'
listen_ip=$(grep '^network.host:' ${config_file}|awk '{print $2}'|awk -F ',' '{print $1}')
PID=$(ps -ef |grep ${app_name}|grep config|grep -v grep|awk '{print $2}')

chk_process(){
    start_pid=$(ps -ef |grep ${app_name}|grep config|grep -v grep|awk '{print $2}')
    port_pid=$(ss -lntp|grep ${app_port}|grep ${listen_ip}|awk -F '=|,' '{print $3}')
    
    if [ -n "${start_pid}" -o -n "${port_pid}" ];then
        if [ -n "${start_pid}" -a -n "${port_pid}" ];then
            if [ "x$start_pid" == "x$port_pid" ];then
               return 0
            else
               return 1
            fi
        elif [ -z "${port_pid}" ];then
            return 2
        fi
        
    else
        return 3
    fi

}

install(){
   #修改环境变量
   descript_msg='Elasticsearch Server'
   id ${run_user} > /dev/null 2>&1 || useradd -r -m -d /var/lib/${run_user} -s /sbin/nologin -c "${descript_msg}"  ${run_user}
   check_sysctl=$(grep 'vm.max_map_count' /etc/sysctl.conf >/dev/null 2>&1;echo $?)
   if [ $check_sysctl -eq 0 ];then
       sed  -i  's/vm.max_map_count.*/vm.max_map_count = 655360/g' /etc/sysctl.conf
   else
       sed  -i  '$avm.max_map_count=655360' /etc/sysctl.conf
   fi
   sysctl -p
   check_memlock=$(grep memlock /etc/security/limits.conf|grep unlimited>/dev/null 2>&1;echo $?)
   if [ $check_memlock -eq 0 ];then  
       sed  -i  's/\*\s*soft\s*memlock.*/* soft memlock unlimited/g'  /etc/security/limits.conf
       sed  -i  's/\*\s*hard\s*memlock.*/* hard memlock unlimited/g'  /etc/security/limits.conf
   else
       sed  -i  '$a\* soft memlock unlimited' /etc/security/limits.conf
       sed  -i  '$a\* hard memlock unlimited' /etc/security/limits.conf
   fi
   check_nofile=$(grep nofile /etc/security/limits.conf|grep 65536 >/dev/null 2>&1;echo $?)
   if [ $check_nofile -eq 0 ];then
       sed  -i  's/\*\s*soft\s*nofile.*/* soft nofile 65536/g'  /etc/security/limits.conf
       sed  -i  's/\*\s*hard\s*nofile.*/* hard nofile 65536/g'  /etc/security/limits.conf
   else
       sed  -i  '$a\* soft nofile 65536' /etc/security/limits.conf
       sed  -i  '$a\* hard nofile 65536' /etc/security/limits.conf
   fi
   data_dir=$(grep  'path.data:' config/elasticsearch.yml |awk '{print $2}')
   log_dir=$(grep  'path.logs:' config/elasticsearch.yml |awk '{print $2}')
   [ -d ${data_dir} ] || mkdir -pv ${data_dir}
   [ -d ${log_dir} ] || mkdir -pv ${log_dir}
   [ -d ${dir}/logs ] || mkdir -pv ${dir}/logs
   chown -R ${run_user}.${run_user} ${dir}  ${data_dir} ${log_dir} 

}

uninstall(){
    date_time=$(date +%Y%m%d%H)
    stop
    cd ${app_data_dir%/*} && mv -vf elasticsearch elasticsearch_old_${date_time} 
    rm -rvf ${app_log_dir}
    rm -rvf ${dir}
}

start(){
    declare -i num=10
    chk_process
    chk_result=$?
    if [ ${chk_result} -eq 3 ];then
        su - ${run_user} -s /bin/bash -c "${dir}/bin/elasticsearch -p ${dir}/elasticsearch.pid -d >/dev/null 2>&1"
        echo -e "starting ${app_name} \c"
        sleep 1
        while [ $num -gt 0 ];do
            chk_process
            chk_result=$?
            if [ ${chk_result} -eq 0 -o ${chk_result} -eq 2 ];then
                echo -e "complete!"
                break
            else
                echo -e ".\c"
            fi
            sleep 1
            let num-=1
        done
        if [ $num -eq 0 ];then
            echo "start faild!"
            stop
        fi
    else
        echo "${app_name} already running! PID:${PID}"
        exit 1
    fi
}

stop(){
    cd  ${dir}
    declare -i num=5
    chk_process
    chk_result=$?

    if [ $chk_result -ne 3 ];then
        if [ -f ${dir}/elasticsearch.pid ];then
            kill -15 $(cat elasticsearch.pid)
        else
            kill -15 ${PID}
        fi 
        sleep 1
        echo -e "stopping ${app_name} \c"
        while [  $num -gt 0 ];do
            chk_process
            chk_result=$?
            if [ $chk_result -eq 3 ];then
                echo "complete!"
                break
            else
                echo -e  '.\c'
                kill -9  ${PID}
            fi
            sleep 1
            let num-=1
        done
    else
        echo "${app_name} are stopped!"
    fi
}

restart(){
    cd ${dir}
    stop
    start
}

status(){
    chk_process
    chk_result=$?
    if [ $chk_result -eq 0 ];then
        echo "${app_name} is running! PID:${PID}"
        exit 0
    else
        exit 1
    fi  
}

case $1 in
    'install')
       install
    ;;
    'uninstall')
       uninstall
    ;;
    'start')
       start
    ;;
    'stop')
       stop
    ;;
    'restart')
        restart
    ;;
    'status')
        status
    ;;
    *)
	echo "Usage: $0 {install | uninstall | start | stop | rstart | status}"
	exit 2
    ;;
esac
